package com.woniuxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.annotation.MyAnnotation;
import com.woniuxy.annotation.PersonRecord;
import com.woniuxy.config.OssUtil;
import com.woniuxy.config.RedisConfig;
import com.woniuxy.dto.JsonResultDto;
import com.woniuxy.entity.Personrecord;
import com.woniuxy.param.UserLoginParam;
import com.woniuxy.param.UserParam;
import com.woniuxy.redisson.RedissonResult;
import com.woniuxy.service.PersonrecordService;
import com.woniuxy.service.UserService;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuyanzu
 * @since 2021-05-31
 */
@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisConfig redisConfig;
    @Autowired
    private RedissonResult redissonResult;
    @Autowired
    private PersonrecordService personrecordService;
    @Autowired
    private RedissonClient redissonClient;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

//    @GetMapping("/test")
//    public String test() throws Exception {
//
//
//    }
    @MyAnnotation
    @PostMapping("/register")
    public JsonResultDto register(UserParam userParam){
        return userService.register(userParam);
    }

    @PersonRecord
    @GetMapping("/login")
    public JsonResultDto login(UserLoginParam userLoginParam){
        return userService.login(userLoginParam);
    }

    @GetMapping("/getRegisterCode")
    public JsonResultDto getRegisterCode(String tel){
        return userService.getRegisterCode(tel);
    }
    //定时器测试
    @GetMapping("/personrecord")
    public void personRecord(){
        Long personDay = redisConfig.opsForHash().size("PersonDay");
        String date = new SimpleDateFormat("yyyyMMdd").format(new Date());
        Personrecord personrecord = new Personrecord();
        personrecord.setpValue(String.valueOf(personDay));
        personrecord.setpDate(date);
        Message<Personrecord> message = MessageBuilder.withPayload(personrecord).build();
        rocketMQTemplate.syncSend("personday:woniu",message);
//        personrecordService.save(personrecord);
//        redisConfig.delete("PersonDay");
    }

    @GetMapping("/personRecordDay")
    public Personrecord personRecordDay(String month,String day) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
        if(Integer.parseInt(month)<10){
            month = "0"+month;
        }
        if(Integer.parseInt(day)<10){
            day = "0"+day;
        }
        String time = format.format(new Date()).substring(0,4)+month+day;
        System.out.println(time);
        return personrecordService.getOne(new QueryWrapper<Personrecord>().eq("p_date",time));
    }
    @GetMapping("/personRecordMonth")
    public Integer personRecordMonth(String month){
        String year = new SimpleDateFormat("yyyyMMdd").format(new Date()).substring(0,4);
        QueryWrapper<Personrecord> wrapper = new QueryWrapper<>();
        if(Integer.parseInt(month)<10){
            month = "0"+month;
        }
        wrapper.le("p_date",year+month+"31");
        wrapper.ge("p_date",year+month+"01");
        List<Personrecord> list = personrecordService.list(wrapper);
        Integer value = 0;
        for(Personrecord personrecord:list){
            value += Integer.parseInt(personrecord.getpValue());
        }
        return value;
    }
    @GetMapping("/ttt")
    public void ttt() throws Exception {
        System.out.println("------");
        RLock lock = redissonClient.getLock("key");
        boolean b = lock.tryLock(20, 20, TimeUnit.SECONDS);
        if(b){
            Integer goodNum = Integer.parseInt((String) redisConfig.opsForValue().get("goods"));
            goodNum = goodNum-1;
            System.out.println(goodNum);
            redisConfig.opsForValue().set("goods",String.valueOf(goodNum));
        }
        lock.unlock();
//        Integer value = Integer.parseInt((String) redisConfig.opsForValue().get("goods"));
//        System.out.println("enter");
    }
    @GetMapping("/getCodeRocket")
    public void getCodeRocket(String tel){
        userService.getCodeRocket(tel);
    }
    @GetMapping("/getCodeRocketTrue")
    public String getCodeRocketTrue(String tel){
        return (String) redisConfig.opsForValue().get(tel);
    }
    @GetMapping("/testS")
    public String testS(){
        System.out.println("enter");
        return "enter";
    }
    @GetMapping("/alipay")
    public void alipay() throws Exception {
        System.out.println("1");
//        PayUtil.alipay("100","200","300","400");
        System.out.println("2");
    }
    @PostMapping("/upload")
    public void upload(@RequestParam(value="file",required=false)MultipartFile[] multipartFile) throws IOException {
        for(MultipartFile file:multipartFile){
            OssUtil.upload(file);
        }
    }
    @GetMapping("/download")
    public void download(String objectName){
        OssUtil.download(objectName);
    }
}




